package com.zw.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zw.commons.utlis.BeanUtil;
import com.zw.object.result.Result;
import com.zw.object.system.SysLoginLog;
import com.zw.log.mapper.SysLoginLogMapper;
import com.zw.log.service.SysLoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.object.vo.LogLoginVo;
import com.zw.object.vo.SysLoginLogQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统访问记录 服务实现类
 * </p>
 *
 * @author zw
 * @since 2022-12-13
 */
@Service
@Slf4j
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    /**
     *                  用户记录用户登录日志业务
     * @param loginVo   参数实体类
     */
    @Override
    public void asyncLoginLogSave(LogLoginVo loginVo) {
        SysLoginLog loginLog = BeanUtil.copyProperties(loginVo, SysLoginLog::new);
        log.info("SysLoginLogServiceImpl.asyncLoginLogSave业务结束,结果:{}",loginLog);
        baseMapper.insert(loginLog);
    }

    /**
     *                              登录日志信息分页获取业务
     *
     * @param iPage                 分页对象
     * @param sysLoginLogQueryVo    参数实体类
     * @return                      Result
     */
    @Override
    public Result selectListPage(IPage<SysLoginLog> iPage, SysLoginLogQueryVo sysLoginLogQueryVo) {
        LambdaQueryWrapper<SysLoginLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotBlank(sysLoginLogQueryVo.getUsername()),SysLoginLog::getUsername,sysLoginLogQueryVo.getUsername());
        if(StringUtils.isNotBlank(sysLoginLogQueryVo.getCreateTimeBegin())&&StringUtils.isNotBlank(sysLoginLogQueryVo.getCreateTimeEnd())){
            queryWrapper.and(userWrapper->userWrapper
                    .ge(SysLoginLog::getCreateTime,sysLoginLogQueryVo.getCreateTimeBegin())
                    .le(SysLoginLog::getStatus,sysLoginLogQueryVo.getCreateTimeEnd()));
        }
        queryWrapper.eq(SysLoginLog::getIsDeleted,0);
        queryWrapper.orderByAsc(SysLoginLog::getCreateTime);
        IPage<SysLoginLog> page = baseMapper.selectPage(iPage, queryWrapper);
        return Result.ok(page);
    }
}
